package com.wml.sellPhone.intercept;

import com.wml.sellPhone.utils.Constant;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户未登录时进行拦截
 *
 * @author Manka
 * @date 2018-06-29 11:02
 */
public class LoginIntercept extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 判断session是否存在
        Object user = request.getSession().getAttribute(Constant.USER_INFO);
        if (user != null){
            return true;
        } else {
//           跳转到首页
            request.setAttribute("error","请先登录再进入购物车");
            request.getRequestDispatcher("/index").forward(request,response);
            return false;
        }
    }
}
